t = int(input())
for _ in range(t):
x = int(input())
arr = list(map(int, input().split()))
a = arr[x - 1]
if a == 0:
print("NO")
else:
b = arr[a - 1]
if b == 0:
print("NO")
else:
print("YES")
#include <bits/stdc++.h>
// #include <bits/stdc++.h>
#define ll long long int
#define dis(a) cout<<a<<"\n";
#define ret(a) {dis(a); return;}
ll expo1(ll a, ll b) {ll res = 1;while (b > 0){if(b & 1)res=(res*a);a=(a*a);b = b >> 1;} return res;}
ll expo(ll a,ll b,ll MOD=1e9+7) {ll res = 1; a%=MOD; while(b>0){if(b&1)res=(res*a)%MOD;a=(a*a)%MOD;b=b>>1;} return res;}
using namespace std;
void solve(){
int n;
cin>>n;
int a[3];
cin>>a[0]>>a[1]>>a[2];
if(a[n-1]!=0 && a[a[n-1]-1]!=0 && a[a[a[n-1]-1]-1]==0) ret("YES")
ret("NO")
}
int main()
{
ios_base::sync_with_stdio(0);
cin.tie(0);cout.tie(0);
int t=1;
cin>>t;
while(t--){
solve();
}
return 0;
}
// 1 2 1 3 4
1433C - Dominant Piranha | 633A - Ebony and Ivory |
1196A - Three Piles of Candies | 299A - Ksusha and Array |
448B - Suffix Structures | 1092B - Teams Forming |
1166C - A Tale of Two Lands | 544B - Sea and Islands |
152B - Steps | 1174D - Ehab and the Expected XOR Problem |
1511A - Review Site | 1316A - Grade Allocation |
838A - Binary Blocks | 1515D - Phoenix and Socks |
1624D - Palindromes Coloring | 1552F - Telepanting |
1692G - 2Sort | 1191A - Tokitsukaze and Enhancement |
903A - Hungry Student Problem | 52B - Right Triangles |
1712A - Wonderful Permutation | 1712D - Empty Graph |
1712B - Woeful Permutation | 1712C - Sort Zero |
1028B - Unnatural Conditions | 735B - Urbanization |
746C - Tram | 1278B - A and B |
1353D - Constructing the Array | 1269C - Long Beautiful Integer |